package com.ncs.leisure.beans.ui;

import java.util.Date;

import com.ncs.leisure.beans.memory.BeansMember;
import com.ncs.leisure.beans.memory.MapList;
import com.ncs.leisure.beans.memory.MapMirror;
import com.ncs.leisure.beans.ui.vo.Map;
import com.ncs.leisure.overall.ui.BasicForm;
import com.ncs.leisure.overall.ui.vo.MemberBean;
import com.ncs.leisure.overall.util.AlgorithmSet;

public class HallForm extends BasicForm{
	
	//注入帐户(测试)
	
	public String inUser(){
		
		this.loadSpring();
		
		MemberBean mb=new MemberBean();
		
		mb.setId(1);
		mb.setUsername("admin");
		mb.setPassword("admin");
		
		session.setAttribute("baseMember",mb);
		
		return "ok";
	}
	
	//显示页面
	
	public String showHall(int nowPage){
		
		this.loadSpring();
		
		MapList mapList=(MapList)ctx.getServletContext().getAttribute("BeansHall");
				
		return mapList.showHall(nowPage);
		
	}
	
	//增加空间
	
	public Integer addRoom(String name,Integer maxNum,boolean locked,String password){
		
		//测试方法
		//inUser();
		//测试方法
		
		this.loadSpring();
		
		//判断已经存在的标记
		
		Integer roomId= (Integer)session.getAttribute("BeansRoomId");
		
		if(roomId!=null){
			
			return -2;
			
		}
		
		//获得用户信息
		
		MemberBean memberBean=(MemberBean)session.getAttribute("baseMember");
		
		MapMirror mapMirror=new MapMirror();
		
		mapMirror.setId(AlgorithmSet.getRandomInteger());
		
		mapMirror.setLocked(locked);
		
		mapMirror.setPassword(password);
		
		mapMirror.setMap(null);
		
		mapMirror.setName(name);
		
		mapMirror.setNum(maxNum);
		
		mapMirror.setCreateTime(new Date());
		
		mapMirror.setOwner(memberBean.getId());
		
		mapMirror.setState("waiting");
		
		//mapMirror.setEffectList(effectList);
		
		BeansMember bm=new BeansMember();
		
		bm.setId(memberBean.getId());
		
		bm.setMember(memberBean.getUsername());
		
		//bm.setRealname(memberBean.);
		
		bm.setState("ready");
		
		mapMirror.getPersonList().put(memberBean.getId(), bm);
		
		mapMirror.setMap((Map)beansContactBusiness.findMapMirrorById(1));
		
		MapList mapList=(MapList)ctx.getServletContext().getAttribute("BeansHall");
		
		session.setAttribute("BeansRoomId", mapMirror.getId());
		
		if(mapList.addMapMirror(mapMirror).equals("ok")){
			
			return mapMirror.getId();
			
		}else{
			
			return -1;
			
		}
		
		
	}
	
	//加入空间
	public Integer joinRoom(Integer mapMirrorId,String pws){
		
		//测试方法
		//inUser();
		//测试方法

		
		this.loadSpring();
		
		//判断已经存在的标记
		
		Integer roomId= (Integer)session.getAttribute("BeansRoomId");
		
		if(roomId!=null){
			
			return -2;
			
		}
		
		MapList mapList=(MapList)ctx.getServletContext().getAttribute("BeansHall");
		
		MapMirror mapMirror=mapList.findMapMirrorByIdObject(mapMirrorId);
		
		if(mapMirror==null){
			
			return -3;
			
		}
		/*
		if(mapMirror.isLocked() && !mapMirror.getPassword().equals(pws)){
			
			return -4;
			
		}
		*/
		MemberBean memberBean=(MemberBean)session.getAttribute("baseMember");
		
		BeansMember bm=new BeansMember();
		
		bm.setId(memberBean.getId());
		
		bm.setMember(memberBean.getUsername());
		
		//bm.setRealname(memberBean.getUsername());
		
		bm.setState("wait");
		
		mapMirror.getPersonList().put(memberBean.getId(), bm);
		
		session.setAttribute("BeansRoomId", mapMirror.getId());
		
		return 0;
		
	}
	

}
